<?php

/**
 * login actions.
 *
 * @package    encuestas
 * @subpackage login
 * @author     qualem
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class loginActions extends sfActions {

    /**
     * Executes index action
     *
     * @param sfRequest $request A request object
     */

    public function executeSingIn(sfWebRequest $request){
        $user = $this->getUser();
       
        if ($user->isAuthenticated()) {
            return $this->redirect('@homepage');
        }

        if ($request->isXmlHttpRequest()) {
            $this->getResponse()->setHeaderOnly(true);
            $this->getResponse()->setStatusCode(401);

            return sfView::NONE;
        }
        $this->form = new LoginForm();
        $this->msg = "";
    }
    public function executeSubmit(sfWebRequest $request){
        $this->form = new LoginForm();
        $this->processForm($this->form, $request);
        $this->setTemplate('singIn');
    }

     
    public function executeLogout(sfWebRequest $request){
        $this->getUser()->logout();
        $this->redirect('@homepage');
    }

    protected function processForm(sfForm $form, sfWebRequest $request){
        $form->bind($request->getParameter($this->form->getName()));
        $this->msg = "";
        if($form->isValid()){
            $email = $form->getValue('email');
            $password = $form->getValue('password');
            $password = Persona::encriptPassword($password);
            $persona = EncPersonaPeer::retrieveByEmail($email);
            if($persona instanceof EncPersona && $persona->getPassword() == $password){
            $this->getUser()->singIn($persona);
            return $this->redirect('@homepage');
            }else  $this->msg = "El email o password son incorrectos";
        }
    }

}
